package com.oocl.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.oocl.activemq.MessageProducerFactory;
import com.oocl.dto.MUserRegDTO;
import com.oocl.exception.MyException;
import com.oocl.pojo.Muser;
import com.oocl.service.MuserService;
import com.oocl.service.impl.MuserServiceImpl;
import com.oocl.util.Base64Util;
import com.oocl.util.FileUtil;
import com.oocl.util.PropertiesFactory;
import com.oocl.util.RequestUtil;

/**
 * Servlet implementation class UpdateMuserServlet
 */
public class UpdateMuserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("UpdateMuserServlet");
		DiskFileItemFactory factory = new DiskFileItemFactory(20480,new File("D:/temp"));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setFileSizeMax(1024000);
		List<FileItem> list = null;
		String name = null;
		String password = null;
		String icnum = null;
		String addr = null;
		String mid = null;
		byte[] portrait =null;
		byte[] idCard =null;
		try {
		list = upload.parseRequest(request);
			String fileName = "";
			
			for (FileItem item : list) {
				if (item.isFormField()) {
					if ("name".equals(item.getFieldName())) {
						name=item.getString();
					} else if ("password".equals(item.getFieldName())) {
						password=item.getString();
					} else if ("icnum".equals(item.getFieldName())) {
						icnum=item.getString();
						fileName = item.getString() + ".png";
						System.out.println(fileName);
					} else if ("addr".equals(item.getFieldName())) {
						addr=item.getString();
					} else if ("mid".equals(item.getFieldName())) {
						mid=item.getString();
					}
				} else {
					InputStream in = item.getInputStream();
					if("portrait".equals(item.getFieldName())&&!"".equals(item.getName())&&null!=item.getName()){
						portrait = new  byte[in.available()];
						in.read(portrait);
					}else if(!"".equals(item.getName())&&null!=item.getName()){
						idCard = new byte[in.available()];
						in.read(idCard);
					}
					in.close();
				}
			}
			String basePath =  PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("portraitfile");
			String portraitPath =basePath+ File.separator + fileName;
			String idCardPath = basePath + File.separator + fileName;
		
			MuserService mSer = new MuserServiceImpl();
			Muser omuser = mSer.loadMuserById(mid);
			if(null!=name){
				omuser.setMname(name);
			}else if (null!=password){
				omuser.setPwd(password);
			}
			else if (null!=icnum){
				omuser.setIcnum(icnum);
				if(null!=portrait){
					FileUtil.toFile(portrait, portraitPath);
				}else{
					FileUtil.toFile(Base64Util.wrapBase64(omuser.getPortrait()), portraitPath);
				}
			}
			else if (null!=addr){
				omuser.setMaddr(addr);
			}
			else if (null!=idCard){
				FileUtil.toFile(idCard, idCardPath);
			}
			
			omuser.setPortrait(fileName);
			int count = mSer.updateMuser(omuser);
			MUserRegDTO mu = new MUserRegDTO();
			mu.setIcNum(omuser.getIcnum());
			mu.setAddr(omuser.getMaddr());
			if(null!=idCard){
				
				mu.setIcPic(Base64Util.wrapBase64Forsavepic(idCard));
			}
			mu.setName(omuser.getMname());
            if(null!=portrait){
				
            	mu.setPortrait(Base64Util.wrapBase64Forsavepic(portrait));
			 }
			mu.setToken(omuser.getToken());
			MessageProducerFactory.newInstance().createMsgProducer().send(RequestUtil.wrapToJson(mu));
			
			response.sendRedirect("success.jsp");
			
	 }catch (MyException e) {
		   throw e;
		} catch (FileUploadException e) {
//			e.printStackTrace();
			throw new MyException("file not found");
		}
	}
}
